package com.ebelter.seneo.ui.activitys;


import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.models.SeneoNet;
import com.ebelter.seneo.utils.SeneoUtils;

import java.net.URLEncoder;
import java.util.Map;


public class FitbitActivity extends SeneoBaseActivity {


    ImageView google_fit_iv;
    ImageView top_iv;
    TextView top_tv1;

    private Handler mHandler=new Handler();



    @Override
    protected int loadLayoutById() {
        return R.layout.activity_fitbit;
    }


    private boolean open;

    @Override
    protected void initView() {
        google_fit_iv=findViewById(R.id.google_fit_iv);
        top_iv=findViewById(R.id.top_iv);
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        top_tv1=findViewById(R.id.top_tv1);
        top_tv1.setText("Fitbit");
        ViewUtils.displayView(top_tv1);



        String token = SeneoUtils.getFitbitToken();

        LogUtils.i2("FitbitActivity","token = "+token);

        if (TextUtils.isEmpty(token)){
            setGoogleFitIvStattion(false);//设置图标为关闭状态
        }else {
            setGoogleFitIvStattion(true);//设置图标为关闭状态
        }



        google_fit_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String token = SeneoUtils.getFitbitToken();
                if (!TextUtils.isEmpty(token)){
                    //如果是开启的 就要关闭掉
                    setGoogleFitIvStattion(false);//设置图标为关闭状态
                    SeneoUtils.setFitbitToken(null);
                }else {
                    //如果是关闭的 就要开启
                    String uri = "https://www.fitbit.com/oauth2/authorize?response_type=token&client_id=22B6YX&redirect_uri=";
                    uri=uri+ URLEncoder.encode("www://seneocallback.com");//oauth%3A%2F%2Fyourcallback.com
                    uri+="&scope=weight&expires_in=31536000";
                    Log.i("FitbitActivity","---编码后的地址 = "+uri);

                    Uri uri2 = Uri.parse(uri);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri2);
                    startActivity(intent);

                }
            }
        });
    }



    @Override
    protected void initData() {
        super.initData();

    }



    private void setGoogleFitIvStattion(boolean open){
        if (open){
            ViewUtils.setImageResource(google_fit_iv,R.drawable.ic_slide_open);
        }else {
            ViewUtils.setImageResource(google_fit_iv,R.drawable.ic_slide_close);
        }
    }





    public void insertWeight(View view) {

        SeneoUtils.uploadWeight2Fitbit(77.2f,23.4f,"2019-08-16 14:40:40");
    }

    public void insertFat(View view) {

    }


}
